package com.yhh.college_information_platform.controller;

import com.yhh.college_information_platform.annotation.RequireAuth;
import com.yhh.college_information_platform.common.PageResult;
import com.yhh.college_information_platform.common.Result;
import com.yhh.college_information_platform.dto.NotificationQueryDTO;
import com.yhh.college_information_platform.service.NotificationService;
import com.yhh.college_information_platform.vo.NotificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 消息通知控制器（所有接口需要登录）
 */
@RestController
@RequestMapping("/notification")
@RequireAuth  // 整个Controller都需要登录
public class NotificationController {
    
    @Autowired
    private NotificationService notificationService;
    
    /**
     * 获取通知列表
     */
    @PostMapping("/list")
    public Result<PageResult<NotificationVO>> getNotificationList(
            @RequestBody NotificationQueryDTO queryDTO,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        PageResult<NotificationVO> result = notificationService.getNotificationList(userId, queryDTO);
        return Result.success(result);
    }
    
    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread/count")
    public Result<Long> getUnreadCount(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Long count = notificationService.getUnreadCount(userId);
        return Result.success(count);
    }
    
    /**
     * 标记单条通知为已读
     */
    @PutMapping("/{id}/read")
    public Result<Void> markAsRead(
            @PathVariable("id") Long notificationId,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        notificationService.markAsRead(notificationId, userId);
        return Result.success();
    }
    
    /**
     * 标记全部通知为已读
     */
    @PutMapping("/read/all")
    public Result<Void> markAllAsRead(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        notificationService.markAllAsRead(userId);
        return Result.success();
    }
    
    /**
     * 删除通知
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteNotification(
            @PathVariable("id") Long notificationId,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        notificationService.deleteNotification(notificationId, userId);
        return Result.success();
    }
    
    /**
     * 清空已读通知
     */
    @DeleteMapping("/read/clear")
    public Result<Void> clearReadNotifications(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        notificationService.clearReadNotifications(userId);
        return Result.success();
    }
}

